package cn.fan.web;

import cn.fan.constants.DataBaseConstants;
import cn.fan.pojo.FundData;
import cn.fan.pojo.simulate.FundMoveAvg;
import cn.fan.pojo.simulate.Trade;
import cn.fan.service.FundDataService;
import cn.fan.utils.MyPage;
import cn.fan.utils.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("funddatas")
public class FundDataController {

    @Autowired
    private FundDataService fundDataService;

    @GetMapping("test")
    public R test(){
        System.out.println("test!!");
        return R.ok("test");
    }

    @GetMapping("synchro/{codes}")
    public R synchro(@PathVariable("codes") String codes){
        try {
            fundDataService.synchro(codes);
            return R.ok("数据已在后台进行同步中！");
        } catch (Exception e) {
            return R.error();
        }
    }

    // 全量同步
    @GetMapping("synchroAll/{codes}")
    public R synchroAll(@PathVariable("codes") String codes){
        try {
            fundDataService.synchroAll(codes);
            return R.ok("数据已在后台进行同步中！");
        } catch (Exception e) {
            return R.error();
        }
    }

    @GetMapping("allDataSynchro")
    public R allDataSynchro(){
        try {
            fundDataService.allDataSynchro();
            return R.ok("数据已在后台进行同步中！");
        } catch (Exception e) {
            return R.error();
        }
    }

    @GetMapping("allDataSynchroAll")
    public R allDataSynchroAll(){
        try {
            fundDataService.allDataSynchroAll();
            return R.ok("数据已在后台进行同步中！");
        } catch (Exception e) {
            return R.error();
        }
    }

    @GetMapping("get/{id}")
    public R get(@PathVariable("id") long id){
        FundData fundData = fundDataService.getById(id);
        if (fundData == null)
            return R.error("无该基金数据");

        Map<String,Object> res =new HashMap<>();
        res.put("fundData",fundData);
        return R.ok(res);
    }

    // 暂时仅支持同步数据，不支持手工添加
//    @PostMapping("add/{code}")
//    public R add(@PathVariable("code") String code){
//        fundMainService.synchro(code);
//        return R.ok();
//    }

    @GetMapping("delete/{id}")
    public R delete(@PathVariable("id") long id){
        int i = fundDataService.delete(id);
        return i> DataBaseConstants.NO_DATA ? R.ok() : R.error("无该基金数据");
    }

    // 暂时也不提供修改接口
//    @PostMapping("edit")
//    public R edit(@RequestBody FundData fundData){
//        int i = fundDataService.edit(fundData);
//        return i> DataBaseConstants.NO_DATA ? R.ok() : R.error("无该基金数据");
//    }

    @PostMapping("list")
    public R list(@RequestBody Map<String,Object> map){
        List<FundData> list = fundDataService.list(map);
        Map<String,Object> res = new HashMap<>();
        res.put("list",list);
        return R.ok(res);
    }

    @PostMapping("list/{pageSize}/{currentPage}")
    public R page(@RequestBody Map<String,Object> map,@PathVariable("pageSize") long pageSize,@PathVariable("currentPage") long currentPage){
        IPage<FundData> page = fundDataService.page(map,currentPage,pageSize);
        MyPage pagination=new MyPage(page);
        Map<String,Object> res = new HashMap<>();
        res.put("pagination",pagination);
        return R.ok(res);
    }

    @PostMapping("moveAvg")
    public R moveAvg(@RequestBody Map<String,Object> map){
        List<FundMoveAvg> list = fundDataService.moveAvg(map);
        Map<String,Object> res = new HashMap<>();
        res.put("list",list);
        return R.ok(res);
    }

    @PostMapping("pandect")
    public R pandect(@RequestBody Map<String,Object> map){
        Map<String,Object> pandect=fundDataService.pandect(map);
        Map<String,Object> res = new HashMap<>();
        res.put("pandect",pandect);
        return R.ok(res);
    }

    @PostMapping("grid")
    public R grid(@RequestBody Map<String,Object> map){
        List<FundData> fundDatas=fundDataService.list(map);
        List<Trade> gridList = fundDataService.gridList(map,fundDatas);
        Map<String, Object> gridStatistics = fundDataService.gridStatistics(map,fundDatas,gridList);
        Map<String,Object> res = new HashMap<>();
        res.put("gridList",gridList);
        res.put("gridStatistics",gridStatistics);
        return R.ok(res);
    }

    @PostMapping("castSurely")
    public R castSurely(@RequestBody Map<String,Object> map){
        Map<String,Object> castSurely=fundDataService.castSurely(map);
        Map<String,Object> res = new HashMap<>();
        res.put("castSurely",castSurely);
        return R.ok(res);
    }
}
